﻿using PivotData;
using PivotIO.XML;
using System.Collections.Generic;
using System.Net;
using System.Web.Http;
using System.Linq;
using System.IO;

namespace PivotWebsite.Controllers
{
    public class CollectionsController : ApiController
    {
        /// <summary>
        /// Get a list of all collections we know about so far
        /// </summary>
        /// <returns></returns>
        public IEnumerable<CollectionInfo> GetAllCollections()
        {
            return CollectionInfoIO.ListAllCollections();
        }

        /// <summary>
        /// Look for the collection by name
        /// </summary>
        /// <param name="cName"></param>
        /// <returns></returns>
        public CollectionInfo GetCollection(string cName)
        {
            var pi = CollectionInfoIO.ListAllCollections().Where(c => c.Name == cName).FirstOrDefault();
            if (pi == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);

            return pi;
        }

        /// <summary>
        /// Return one of the collections when we look up by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CollectionInfo GetCollectionById(int id)
        {
            var pi = CollectionInfoIO.LoadCollection(id);
            if (pi == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);
            return pi;
        }

        /// <summary>
        /// Put a new collection
        /// </summary>
        /// <param name="cName"></param>
        /// <returns></returns>
        public CollectionInfo PutCollection(string cName)
        {
            if (cName == null)
                throw new HttpResponseException(HttpStatusCode.BadRequest);

            var oldCollection = CollectionInfoIO.LoadCollection(cName);
            if (oldCollection == null)
            {
                var newCollection = new CollectionInfo() { Name = cName, Id = CollectionInfoIO.GetMaxCollectionId() + 1 };
                CollectionInfoIO.SaveCollection(newCollection);
                return newCollection;
            }
            else
            {
                return oldCollection;
            }
        }

        /// <summary>
        /// Delete a collection by Id.
        /// </summary>
        /// <param name="cId"></param>
        public void DeleteCollection(int cId)
        {
            var ci = CollectionInfoIO.LoadCollection(cId);
            DeleteCollection(ci);
        }

        /// <summary>
        /// Delete a collection by name
        /// </summary>
        /// <param name="cName"></param>
        public void DeleteCollection(string cName)
        {
            var ci = CollectionInfoIO.LoadCollection(cName);
            DeleteCollection(ci);
        }

        /// <summary>
        /// Actually do the delete work.
        /// </summary>
        /// <param name="ci"></param>
        private static void DeleteCollection(CollectionInfo ci)
        {
            if (ci != null)
            {
                try
                {
                    CollectionInfoIO.DeleteCollection(ci);
                }
                catch { }
                try
                {
                    ImageInfoIO.DeleteAllImages(ci.Id);
                }
                catch { }
                try
                {
                    var wdir = new DirectoryInfo(Path.Combine(Utils.GetWebDataDirectory().FullName, ci.Id.ToString()));
                    if (wdir.Exists)
                        wdir.Delete(true);
                }
                catch { }
            }
        }
    }
}
